<template>
	<view class="register">
		<uni-nav-bar @clickLeft="Back" backgroundColor="transparent" left-icon="back" color="#fff" title="注册" :shadow="false" :border="false" ></uni-nav-bar>
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logo"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput
					v-model="username"
					type="text"
					placeholder="用户名"
				></wInput>
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="手机号"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="登录密码"
					isShowPass
				></wInput>
				<wInput
					v-model="verCode"
					type="number"
					maxlength="6"
					placeholder="验证码"
					
					isShowCode
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
					
			</view>
				
			<wButton 
				class="wbutton"
				text="注 册"
				:rotate="isRotate" 
				@click.native="startReg()"
			></wButton>
			
			<!-- 底部信息 -->
			<view class="footer">
				<text 
					@tap="isShowAgree" 
					class="cuIcon"
					:class="showAgree?'cuIcon-radiobox':'cuIcon-round'"
				> 同意</text>
				<!-- 协议地址 -->
				<navigator url="protocol" open-type="navigate">《协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				logo:require('../../static/image/login/logo.png'),
				//logo图片 base64
				phoneData:'', // 用户/电话
				username:'',//用户名
				passData:'', //密码
				verCode:"", //验证码
				showAgree:true, //协议是否选择
				isRotate: false, //是否加载旋转
				timer:null,//倒计时
			}
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
		},
		methods: {
			Back(){
				uni.navigateBack({})
			},
			isShowAgree(){
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			getVerCode(){
				//获取验证码
				if (_this.phoneData.length != 11) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
				console.log("获取验证码")
				this.getAuthCode();
			},
			// 获取验证码
			getAuthCode(){
				// 验证手机号
				if (!this.phoneData || this.phoneData.length !== 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请输入正确的手机号'
					});
					return;
				}
				
				this.$api.getAuthCode({
					telephone: this.phoneData,
				}).then(res => {
					console.log('验证码响应', res);
					
					// 处理响应数据
					if (res.data && res.data.code === 200) {
						clearTimeout(this.timer);
						this.$refs.runCode.$emit('runCode'); //触发倒计时
						
						const responseData = res.data.data;
						let message = '验证码已发送';
						
						// 开发环境会返回验证码
						if (responseData && responseData.code) {
							message = '验证码已发送：' + responseData.code;
							console.log('验证码（开发环境）：', responseData.code);
						}
						
						uni.showToast({
							icon: 'success',
							position: 'bottom',
							title: message
						});
						
						// 60秒后可以重新获取
						setTimeout(function(){
							_this.$refs.runCode.$emit('runCode', 0); // 终止倒计时
						}, 60000);
					} else {
						uni.showToast({
							icon: 'none',
							position: 'bottom',
							title: res.data?.message || res.data?.msg || '获取验证码失败'
						});
					}
				}).catch(err => {
					console.error('获取验证码失败', err);
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '获取验证码失败，请重试'
					});
				});
			},
		    startReg() {
				//注册
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请先同意《协议》'
				    });
				    return false;
				}
				if (!this.username.length) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
				if (this.phoneData.length !=11) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
		        if (this.passData.length < 6) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码大于6位'
		            });
		            return false;
		        }
				if (!this.verCode.length) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请输入验证码'
				    });
				    return false;
				}
				// console.log("注册成功");
				this.confirmRegister();
				
		    },
			confirmRegister(){
				_this.isRotate=true
				this.$api.register({
					username:this.username,
					telephone:this.phoneData,
					password:this.passData,
					authCode:this.verCode,
				}).then(res=>{
					_this.isRotate=false
					if(res.data.code == 200){
						uni.navigateTo({
							url:'./login'
						})
					}
					uni.showToast({
					    icon: 'none',
						position: 'bottom',
					    title: res.data.msg
					});
				})
			}
		}
	}
</script>

<style lang="less" scoped>
@import url("../../components/watch-login/css/icon.css");
@import url("./css/main.css");
.register{
	width: 100vw;
	height: 100vh;
	background: url(../../static/image/login/bg.jpg) no-repeat;
	background-size: cover;
	overflow: auto;
	box-sizing: border-box;
	padding-bottom: 20rpx;
}
.content{
	.header{
		margin-top:40rpx;
	}
}
.wbutton{
	margin-top: 48rpx;
}
.footer{
	margin-top: 30rpx;
}
</style>